package com.geostar.constructionland.statistics.dao;

import com.geostar.constructionland.statistics.mapping.QueryMappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author makoto_zhu
 * @Date 2020-11-18 11:44:43
 * @Description
 **/
@Repository
public class DataQueryDao {
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public List<Map<String, Object>> execute(QueryMappedStatement statement) {
        SqlCommandType commandType = statement.getSqlStatement().getSqlCommandType();
        if (!commandType.equals(SqlCommandType.SELECT)) {
            throw new RuntimeException("sql in not a DQL structure. SQL:" + statement.getSqlStatement().getSql());
        }
        return namedParameterJdbcTemplate.queryForList(statement.getSqlStatement().getSql(), statement.getQueryParam());
    }

}
